<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>用户管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#no").focus();
			$("#inputForm").validate({
				rules: {
					account: {
						remote: "${ctx}/sys/user/checkLoginNameUser?app=${user.app}&&id=${user.id}",
						type: "post",  
						data: {                     //要传递的数据
							loginName: function() {
								return $("#account").val();
							}
						}
					}
				},
				messages: {
					account: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"}
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function addRow(list, idx, tpl, row){

			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$('select').select2();
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			$(obj).parent().parent().remove();
		}
	
		
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/sys/user/listJJR">网点工作人员管理</a></li>
		<li class="active"><a href="${ctx}/sys/user/formTrade?id=${user.id}">网点工作人员${not empty user.id?'修改':'添加'}</a></li>
	</ul>
 	
	
	<br/>
	<form:form id="inputForm" modelAttribute="user" action="${ctx}/sys/user/saveTrader" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<form:hidden path="app" value="nto"/>
		<form:hidden path="userType" />
		<sys:message content="${message}"/>
		<div class="control-group">
			<label class="control-label">头像:</label>
			<div class="controls">
                <form:hidden id="nameImage" path="photo" htmlEscape="false" maxlength="255" class="input-xlarge"/>
               	<sys:ckImage input="nameImage" type="images" uploadPath="/cms/site"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">登录名称:</label>
			<div class="controls">
				<form:input path="account" name="account"  readonly="false" htmlEscape="false" maxlength="32" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">名称:</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="32" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">性别:</label>
			<div class="controls">
				<form:select path="sex" class="input-xlarge required required">
					<form:options items="${fns:getSexList()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">电话:</label>
			<div class="controls">
				<form:input path="phone" htmlEscape="false" maxlength="15" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">密码:</label>
			<div class="controls">
				<input id="newPassword" name="newPassword" type="password" value="" maxlength="50" minlength="3" class="${empty user.id?'required':''}"/>
				<c:if test="${empty user.id}"><span class="help-inline"><font color="red">*</font> </span></c:if>
				<c:if test="${not empty user.id}"><span class="help-inline">若不修改密码，请留空。</span></c:if>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">确认密码:</label>
			<div class="controls">
				<input id="confirmNewPassword" name="confirmNewPassword" type="password" value="" maxlength="50" minlength="3" equalTo="#newPassword"/>
				<c:if test="${empty user.id}"><span class="help-inline"><font color="red">*</font> </span></c:if>
			</div>
		</div>
	
		<div class="control-group">
				<label class="control-label">关联企业：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th width="50">企业</th>
								<th width="10">&nbsp;操作</th>
							</tr>
						</thead>
						<tbody >
						<tbody id="userAndInstituionList">
							<c:forEach items="${user.userAndInstituionList}" var="column" varStatus="vs">
								<tr>
									<td class="hide">
										<input id="userAndInstituionList[${vs.index}].id" name="userAndInstituionList[${vs.index}].id" type="hidden" value="${column.id}"/>
										<input id="userAndInstituionList[{${vs.index}].delFlag" name="userAndInstituionList[${vs.index}].delFlag" type="hidden" value="0"/>
										<input id="userAndInstituionList[{${vs.index}].invite" name="userAndInstituionList[${vs.index}].invite" type="hidden" value="0"/>
									</td>
									<td>
									<select name="userAndInstituionList[${vs.index}].cmInstitutionId" class="required input-mini">
											<c:forEach items="${fns:getInstitutionList()}" var="dict">
												<option value="${fns:escapeHtml(dict.id)}" ${fns:escapeHtml(dict.id)==column.cmInstitutionId?'selected':''} >${fns:escapeHtml(dict.institutionName)}</option>
											</c:forEach>
									</select>
									</td>
									<td class="text-center" width="10">
										删除<span class="close" onclick="delRow(this, '#userAndInstituionList[${vs.index}]')" title="删除">&times;</span>
									</td>
								</tr>
							</c:forEach>
						</tbody>
							<tr><td colspan="5"><a href="javascript:" onclick="addRow('#userAndInstituionList', tUpdateRowIdx, tUpdateTpl);tUpdateRowIdx = tUpdateRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot>
					</table>
					<script type="text/template" id="tUpdateTpl">
						<tr>
							<td class="hide">
								<input id="userAndInstituionList[{{idx}}].id" name="userAndInstituionList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="userAndInstituionList[{{idx}}].invite" name="userAndInstituionList[{{idx}}].invite" type="hidden" value="{{row.invite}}"/>
								<input id="userAndInstituionList[{{idx}}].delFlag" name="userAndInstituionList[{{idx}}].delFlag" type="hidden" value="0"/>

							</td>
							<td>
								<select name="userAndInstituionList[{{idx}}].cmInstitutionId" class="required input-mini">
									<c:forEach items="${fns:getInstitutionList()}" var="dict">
										<option value="${fns:escapeHtml(dict.id)}" >${fns:escapeHtml(dict.institutionName)}</option>
									</c:forEach>
								</select>
								{{row.cmInstitutionId}}
							</td>
							<td class="text-center" width="10">
								删除<span class="close" onclick="delRow(this, '#userAndInstituionList[{{idx}}]')" title="删除">&times;</span>
							</td>
						</tr>
					</script>
					<script type="text/javascript">
						var tUpdateRowIdx = ${user.userAndInstituionList.size()}, tUpdateTpl = $("#tUpdateTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
					</script>
				</div>
		</div>
		
		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>